{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Ducks\n", "\n", "## By Doug Blank\n", "\n" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " Sketch #34:
\n", "
\n", "
\n", "
\n", " \n", " \n", " \n", " \n", "
\n", "Sketch #34 state: Loading...
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "Duck [] ducks;\n", "\n", "int cwidth = 800;\n", "int cheight = 600;\n", "\n", "class Duck {\n", " int x;\n", " int y;\n", " int width;\n", " int height;\n", " color feather_color;\n", " int direction;\n", " int pause;\n", " \n", " Duck(int x, int y, int width, int height) {\n", " this.x = x;\n", " this.y = y;\n", " this.width = width;\n", " this.height = height;\n", " if (random() < .33) {\n", " this.feather_color = color(255, 255, 0);\n", " } else if (random() < .66) {\n", " this.feather_color = color(120, 0, 0);\n", " } else {\n", " this.feather_color = color(128, 128, 128);\n", " }\n", " if (random() < .5) {\n", " this.direction = 0; // left\n", " } else {\n", " this.direction = 1; // right\n", " } \n", " this.pause = 0;\n", " // 2 left turning right\n", " // 3 right turning left\n", " }\n", " \n", " void draw() {\n", " // duck head\n", " fill(this.feather_color);\n", " ellipse(this.x, this.y, this.width, this.width);\n", " // body\n", " rect(this.x - this.width/2, this.y + this.width/2, this.width, this.height - this.width/2);\n", " // duck bill\n", " fill(255, 128, 0); // orange\n", " if (this.direction == 0) { // left\n", " rect(this.x - this.width, this.y - 5, this.width/2, 5);\n", " fill(255);\n", " ellipse(this.x - this.width/3, this.y - this.width/3, 10, 10);\n", " } else if (this.direction == 1) { // right\n", " rect(this.x + this.width/2, this.y - 5, this.width/2, 5);\n", " fill(255);\n", " ellipse(this.x + this.width/3, this.y - this.width/3, 10, 10);\n", " } else {\n", " rect(this.x - 10, this.y - 5, this.width/4, 5);\n", " fill(255);\n", " ellipse(this.x + this.width/3, this.y - this.width/3, 10, 10);\n", " ellipse(this.x - this.width/3, this.y - this.width/3, 10, 10);\n", " }\n", " }\n", " void toggle() {\n", " if (this.direction == 0) {\n", " this.direction = 2;\n", " this.pause = 0;\n", " } else if (this.direction == 1) {\n", " this.direction = 3;\n", " this.pause = 0;\n", " }\n", " }\n", " \n", " void waddle() {\n", " // don't move\n", " if (this.x - this.width/2 < 0) {\n", " this.toggle();\n", " this.x = this.width/2;\n", " }\n", " if (this.x + this.width/2 > cwidth) {\n", " this.toggle();\n", " this.x = cwidth - this.width/2;\n", " }\n", " if (this.direction == 0) {\n", " this.x = this.x - this.width/10;\n", " } else if (this.direction == 1){\n", " this.x = this.x + this.width/10;\n", " } else if (this.direction == 2 && this.pause > 20) {\n", " this.direction = 1;\n", " } else if (this.direction == 3 && this.pause > 20) {\n", " this.direction = 0;\n", " }\n", " this.pause = this.pause + 1;\n", " }\n", "}\n", "\n", "void setup() {\n", " size(cwidth, cheight);\n", " \n", " ducks = new Duck[100];\n", " for (int i = 0; i < ducks.length; i++) {\n", " ducks[i] = new Duck(random(cwidth), random(cheight), random(10, 70), random(10, 70));\n", " }\n", "}\n", "\n", "void draw() {\n", " background();\n", " for (int i = 0; i < ducks.length; i++) {\n", " ducks[i].waddle();\n", " ducks[i].draw();\n", " }\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Calysto Processing", "language": "processing", "name": "calysto_processing" }, "language_info": { "codemirror_mode": { "name": "text/x-java", "version": 2 }, "file_extension": ".java", "mimetype": "text/x-java", "name": "java" } }, "nbformat": 4, "nbformat_minor": 0 }